package com.saver.core.persistence;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.saver.core.business.ParamTransaccion;
import com.saver.core.exception.HTTPException;
import com.saver.core.processor.util.Util;
import com.saver.core.util.Constantes;

public class SMSRestService extends AbstractRestServiceJSON {
	
	private static final String TAG = "SMSRestService";

	public SMSRestBean obtenerDatos (Map<String, String> parametros) 
			throws IOException, HTTPException {
		
		SMSRestBean smsRestBean = null;
		
		try {
			JSONObject jsonObject = obtenerJSONObject(Constantes.PATH_SMS_DATOS, HTTPMethod.POST, parametros);
			smsRestBean = crearSMSRestBean(jsonObject);
			
		} catch (ClientProtocolException e) {
			Log.e(TAG, "DATOS: Error de protocolo", e);
		} catch (JSONException e) {
			Log.e(TAG, "DATOS: Error de formato json", e);
		}
		return smsRestBean;
	}
	
	public RespuestaSMSRestBean obtenerListaDatos (String usuario, Collection<ParamTransaccion> parametros)
			throws IOException, HTTPException {

		RespuestaSMSRestBean respuestaSMSRestBean = new RespuestaSMSRestBean();
		
		try {
			JSONArray arrayParams = new JSONArray();
			for (ParamTransaccion param : parametros) {
				JSONObject jsonObject = new JSONObject ();
				jsonObject.put("fsms", param.getFsms());
				jsonObject.put("ftra", param.getFtra());
				jsonObject.put("iba", param.getIba());
				jsonObject.put("ica", param.getIca());
				jsonObject.put("ice", param.getIce());
				jsonObject.put("id", param.getId());
				jsonObject.put("iee", param.getIee());
				jsonObject.put("ies", param.getIes());
				jsonObject.put("ime", param.getIme());
				jsonObject.put("imo", param.getImo());
				jsonObject.put("mnt", param.getMnt());
				jsonObject.put("nce", param.getNce());
				jsonObject.put("nee", param.getNee());
				jsonObject.put("nme", param.getNme());
				jsonObject.put("num", param.getNum());
				jsonObject.put("tip", param.getTip());
				arrayParams.put(jsonObject);
			}
			
			Map<String, String> mapParams = new HashMap<String, String>();
			mapParams.put(Constantes.PARAM_USUARIO, usuario);
			mapParams.put(Constantes.PARAM_TRANS, arrayParams.toString());
			
			JSONObject jsonObject = obtenerJSONObject(Constantes.PATH_SMS_LISTA, HTTPMethod.POST, mapParams);
			String codigoUsuario = Util.getString(jsonObject, "cus");
			JSONArray jsonArray = jsonObject.getJSONArray("lre");
			
			ArrayList<SMSRestBean> smsRestBeans = new ArrayList<SMSRestBean> ();
		
			for (int i=0, n=jsonArray.length(); i<n; i++) {
				SMSRestBean smsRestBean = crearSMSRestBean(jsonArray.getJSONObject(i));
				smsRestBeans.add(smsRestBean);
			}
			
			respuestaSMSRestBean.setListaResultado(smsRestBeans);
			respuestaSMSRestBean.setCodigoUsuario(codigoUsuario);
			
		} catch (ClientProtocolException e) {
			Log.e(TAG, "LISTA: Error de protocolo", e);
		} catch (JSONException e) {
			Log.e(TAG, "LISTA: Error de formato json", e);
		}
		
		return respuestaSMSRestBean;
	}

	private SMSRestBean crearSMSRestBean(JSONObject jsonObject)
			throws IOException, HTTPException, JSONException {
		
		SMSRestBean smsRestBean = new SMSRestBean();
		smsRestBean.setIdObjeto(Util.getInteger(jsonObject, "ido"));
		smsRestBean.setIdCategoria(Util.getInteger(jsonObject, "ica"));
		smsRestBean.setIdCategoriaServ(Util.getInteger(jsonObject, "icv"));
		smsRestBean.setIdEstablecimiento(Util.getInteger(jsonObject, "ies"));
		smsRestBean.setIdEstablecimientoServ(Util.getInteger(jsonObject, "iev"));
		smsRestBean.setIdMoneda(Util.getInteger(jsonObject, "imo"));
		smsRestBean.setIdMonedaServ(Util.getInteger(jsonObject, "imv"));
		smsRestBean.setNombreCategoria(Util.getString(jsonObject, "nca"));
		smsRestBean.setNombreEstablecimiento(Util.getString(jsonObject, "nes"));
		smsRestBean.setNombreMoneda(Util.getString(jsonObject, "nmo"));
		smsRestBean.setCambio(Util.getShort(jsonObject, "cmb"));
		smsRestBean.setExpresion(Util.getString(jsonObject, "exp"));
		smsRestBean.setIdTransaccionServ(Util.getInteger(jsonObject, "itv"));
		
		return smsRestBean;
	}

}
